package com.oocl.crm.repository.impl;

import com.oocl.crm.db.MyLinkedList;
import com.oocl.crm.db.MyLinkedListFactory;
import com.oocl.crm.pojo.Customer;
import com.oocl.crm.repository.CustomerDataProcess;
import com.oocl.crm.util.FormatCmd;

public class CustomerDataProcessImpl implements CustomerDataProcess {
	private MyLinkedList<Customer> db;
	private static int idCounter;
	public CustomerDataProcessImpl() {
		db = MyLinkedListFactory.getInstance();
		idCounter = 10;
	}
	
	@Override
	public void addCustoemr(Customer c) {
		db.insertNode(c);
		idCounter++;
	}

	@Override
	public void updateCustomer(Customer c) {
		// TODO Auto-generated method stub
		int index = Integer.parseInt(c.getId())-1;
		Customer originCustomer = db.getNodeValue(index);
		if(c.getName() != null){
			originCustomer.setName(c.getName());
		}
		if(c.getAddress() != null){
			originCustomer.setAddress(c.getAddress());
		}
		if(c.getBirthday() != null){
			originCustomer.setBirthday(c.getBirthday());
		}
		if(c.getCell() != null){
			originCustomer.setCell(c.getCell());
		}
		if(c.getSex() != null){
			originCustomer.setSex(c.getSex());
		}
	}

	@Override
	public void deleteCustomer(int id) {
		// TODO Auto-generated method stub
//		db.delteNode(id);
		String idString = id+"";
		for(int i=0; i<db.getLength(); i++){
			
			if(idString.equals(db.getNodeValue(i).getId())){
				db.delteNode(i+1);
			}
		}
	}

	@Override
	public Customer[] find(String condition) {
		String[] con = FormatCmd.cmdCondition(condition);
		int count = 0;
		Customer[] customers = new Customer[db.getLength()];
		if(con == null){
			for(int i=0; i<db.getLength(); i++){
				customers[count++]=db.getNodeValue(i);
			}
		}else if(con.length == 1){//单一条件查找
				String[] kv = FormatCmd.cmdKeyValue(con[0]);
				String string = kv[1];
				switch (kv[0]) {
				case "id":
					string = kv[1];
					for(int i=0; i<db.getLength(); i++){
						if(string.equals(db.getNodeValue(i).getId())){
							customers[count] = db.getNodeValue(i);
							count++;
						}
					}
					return customers;
				case "name":
					string =kv[1];
					for(int i=0; i<db.getLength(); i++){
						if(string.equals(db.getNodeValue(i).getName())){
							customers[count] = db.getNodeValue(i);
							count++;
						}
					}
					return customers;
				case "sex":
					string =kv[1];
					for(int i=0; i<db.getLength(); i++){
						if(string.equals(db.getNodeValue(i).getSex())){
							customers[count] = db.getNodeValue(i);
							count++;
						}
					}
					return customers;
				case "address":
					string =kv[1];
					for(int i=0; i<db.getLength(); i++){
						if(string.equals(db.getNodeValue(i).getAddress())){
							customers[count] = db.getNodeValue(i);
							count++;
						}
					}
					return customers;
				case "cell":
					string =kv[1];
					for(int i=0; i<db.getLength(); i++){
						if(string.equals(db.getNodeValue(i).getCell())){
							customers[count] = db.getNodeValue(i);
							count++;
						}
					}
					return customers;
				case "birthday":
					string =kv[1];
					for(int i=0; i<db.getLength(); i++){
						if(string.equals(db.getNodeValue(i).getBirthday())){
							customers[count] = db.getNodeValue(i);
							count++;
						}
					}
					return customers;
				default:
					break;
				}
			}else{//复合条件：按name和sex查找
				String[] kv1 = FormatCmd.cmdKeyValue(con[0]);
				String[] kv2 = FormatCmd.cmdKeyValue(con[1]);
				for(int i=0; i<db.getLength(); i++){
					if(kv1[1].equals(db.getNodeValue(i).getName()) && kv2[1].equals(db.getNodeValue(i).getSex())){
						customers[count] = db.getNodeValue(i);
					}
				}
				return customers;
		}
		return customers;
	}

	@Override
	public Customer[] sort(String filed) {
		// TODO Auto-generated method stub
		Customer[] customers = new Customer[db.getLength()];
		MyLinkedList<Customer> db2 = new MyLinkedList<>();
		for(int i=0; i<db.getLength(); i++){
			Customer customer=db.getNodeValue(i);
			db2.insertNode(customer);
			customers[i] = customer;
		}
		if(filed.equals("id")){
			return customers;
		}else{
			db2.sort();
			for(int i=0; i<db2.getLength(); i++){
				customers[i]=db2.getNodeValue(i);
			}
			return customers;
		}
	}

	@Override
	public int getCounter() {
		// TODO Auto-generated method stub
		return idCounter;
	}

}
